package com.project.personal_blog.garbage_classification_management_system.service.Impl;

import com.project.personal_blog.garbage_classification_management_system.mapper.registerMapper;
import com.project.personal_blog.garbage_classification_management_system.pojo.Code;
import com.project.personal_blog.garbage_classification_management_system.pojo.Result;
import com.project.personal_blog.garbage_classification_management_system.pojo.User;
import com.project.personal_blog.garbage_classification_management_system.service.RegisterService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class registerServiceImpl implements RegisterService {
    @Autowired
    private registerMapper registerMapper;
    private PasswordEncoder passwordEncoder;
    @Override
    public Object register(String username, @NotNull String password, String email, Long phoneNumber) {
        if (password.length() < 8 || password.length() > 16) {
            return  new Result("fail", Code.PASSWORD_LENGTH_ERROR, "密码长度必须在8到16位之间");// 返回null或抛出异常，取决于你的业务逻辑
        }
        if (!password.matches("^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!@#$%^&*]).{8,16}$")) {
            return new Result("fail", Code.PASSWORD_FORMAT_ERROR, "密码必须包含字母、数字和符号");// 返回null或抛出异常，取决于你的业务逻辑
        }
        // 加密密码
        String encodedPassword = passwordEncoder.encode(password);
        User registered = registerMapper.register(username, encodedPassword, email, phoneNumber);
        // 创建用户的其他逻辑
        if (registered != null) {
            return new Result("success", Code.REGISTER_SUCCESS,registered);
        } else {
            return new Result("fail", Code.REGISTER_FAIL, "注册失败");
        }
    }
}
